package Hibernate.ManytoMay.Test;

import org.hibernate.Session;
import org.hibernate.Transaction;

public class Main
	{

		public static void main(String[] args) throws Exception
			{
				HibernateUtil hibernateUtil = new HibernateUtil();
				// hibernateUtil
				// .executeSQLCommand("create table event(event_id integer,event_date date, title varchar(10))Engine=InnoDB");
				// hibernateUtil
				// .executeSQLCommand("create table person(person_id integer,firstname varchar(10), lastname varchar(10),age integer)Engine=InnoDB");
				// hibernateUtil
				// .executeSQLCommand("create table PERSON_EVENT(event_id integer, person_id integer)Engine=InnoDB");
				Session session = hibernateUtil.getSession();
				Transaction tx = session.beginTransaction();

				Event event1 = new Event();
				event1.setTitle("event1");
				Event event2 = new Event();
				event2.setTitle("event2");

				Person person1 = new Person();
				person1.setFirstname("person1");
				Person person2 = new Person();
				person2.setFirstname("person2");

				person1.getEvents().add(event1);
				person1.getEvents().add(event2);

				person2.getEvents().add(event1);
				person2.getEvents().add(event2);

				session.save(event1);
				session.save(event2);

				session.save(person1);
				session.flush();

				// person1.getEvents().clear();

				session.save(person2);
				session.flush();
				tx.commit();

				// session.close();
				hibernateUtil.checkData("select * from event");
				hibernateUtil.checkData("select * from person");
				hibernateUtil.checkData("select * from PERSON_EVENT");
			}
	}
